<?php

namespace App;

use Laravel\Passport\HasApiTokens;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Config;
use App\Models\UserExtension;
use Illuminate\Database\Eloquent\SoftDeletes;


class User extends Authenticatable
{
    use SoftDeletes;
    use Notifiable;
    use HasApiTokens;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $dates = ['delete_at'];


    /**
     * 获取与用户关联的扩展表
     */
    public function userExtensions()
    {
        return $this->hasOne('App\Models\UserExtension','user_id','id');
    }

    public function getUsersInfo()
    {
        $users = $this
                    ->paginate(Config::get('3cs.DEFAULT_PAGE_NUMBER'));

        $data['total'] =$users->total();
        $data['perPage'] =$users->perPage();
        $data['currentPage'] =$users->currentPage();
        $items = [];

        foreach ($users as $key => $user){

            $items[$key] =$this->getUserExtensions($user->userExtensions);
            $items[$key]['id'] =$user->id;
            $items[$key]['name'] =$user->name;
            $items[$key]['email'] =$user->email;

        }

        $data['items'] =$items;

        return $data;
    }

    /*
     * 获取用户和用户扩展信息
     *
     */

    public function getUserInfo($id)
    {
        $user = $this
//            ->select('name','email')
            ->find($id);
        if ($user){
            $data =$this->getUserExtensions($user->userExtensions);
            $data['name'] =$user->name;
            $data['email'] =$user->email;
        }else{
            $data = [];
        }

        return $data;
    }

    /*
     * 更新用户扩展信息
     *
     */

    public function updateUserInfo($request, $id)
    {
//        $user = $this->find($id);
        $userExtension = UserExtension::where('user_id', $id)->first();

        $userExtension->nickname = empty($request['nickname'])?$userExtension->nickname:$request['nickname'];
        $userExtension->wechat = empty($request['wechat'])?$userExtension->wechat:$request['wechat'];
        $userExtension->phone = empty($request['phone'])?$userExtension->phone:$request['phone'];
        $userExtension->sex = empty($request['sex'])?$userExtension->sex:$request['sex'];

        return $userExtension->save();

    }

    /*
     * 转换单个用户扩展信息
     *
     */
    public function getUserExtensions($userExtensions = null)
    {
        $items = [];
        if (isset($userExtensions)){
            $items['nickname'] =$userExtensions->nickname;
            $items['wechat'] =$userExtensions->wechat;
            $items['qq'] =$userExtensions->qq;
            $items['phone'] =$userExtensions->phone;
//            $items['sex'] =getSexStatus($userExtensions->sex);
            $items['sex'] =($userExtensions->sex);
//            $items['status'] =$userExtensions->status;
        }else{
            $items['nickname'] = '';
            $items['wechat'] = '';
            $items['qq'] = '';
            $items['phone'] = '';
            $items['sex'] = '';
//            $items['status'] = '';
        }
        return $items;
    }

}
